﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UIManger : MonoBehaviour {
    //获得界面UI的引用
    public GameObject gameStart;
    public GameObject gaming;
    public GameObject gameEnd;
    public GameObject gameSettings;
    public GameObject gameScoreList;

    //是否开启声音
    private bool IsOnVoice = true;

    //获得摄像机上的声音
    private AudioSource m_Audio;

    //获取到飞龙身上的控制脚本
    private DragonColler m_DragonColler;
    //获取到Bg的脚本
    private TestMove m_TestMove;
    private TestMove m_TestBackMove;

    //获取到柱子的脚本
    public TestHGmove m_HGMove_1;
    public TestHGmove m_HGMove_2;
    
    //获取到飞龙和两根柱子的GameObject的引用
    private GameObject m_FlyDragon;
    private GameObject m_HG_1;
    private GameObject m_HG_2;


    //获取到分数的文本
    public Text gameScore;
    //获取到最后游戏结束的总分数
    public Text gameOver_Score;

    //控制得到的分数
    private int score = 0;

    private GameState gameState = GameState.START;
    //枚举
    public enum GameState
    { 
        START,
        GAMING,
        END,
        SETTING,
        SCORELIST
    }
	void Start () {
        //找到飞龙和柱子
        m_FlyDragon = GameObject.Find("Player");
        m_HG_1 = GameObject.Find("HGitem");
        m_HG_2 = GameObject.Find("HGitem2");
        //获取声音
        m_Audio = GameObject.Find("Main Camera").GetComponent<AudioSource>();

        //取到龙和柱子身上脚本
        m_DragonColler = m_FlyDragon.GetComponent<DragonColler>();
        m_TestMove = GameObject.Find("BG1").GetComponent<TestMove>();
        m_TestBackMove = GameObject.Find("Back").GetComponent<TestMove>();
        //委托注册事件
        m_DragonColler.AddScore += add_Score;
        m_DragonColler.DragonDie += Dragon_Die;

        m_Audio.Stop();
	}
	
	
	void Update () {
        //调用方法
        ChangUI(gameState);
	}

    public void ChangUI(GameState gameState)
    {
        if (gameState == GameState.START)//游戏开始界面
        {
            gameStart.SetActive(true);
            gaming.SetActive(false);
            gameEnd.SetActive(false);
            gameSettings.SetActive(false);
            gameScoreList.SetActive(false);
            //龙是否飞
            m_DragonColler.IsDragonFly(false);
            //BG和黄瓜柱子和Back的控制
            m_HGMove_1.IsMoveZhu(false);
            m_HGMove_2.IsMoveZhu(false);
            m_TestMove.IsBgMove(false);
            m_TestBackMove.IsBgMove(false);
           
        }
        if (gameState == GameState.GAMING)//游戏进行中
        {
            gameStart.SetActive(false);
            gaming.SetActive(true);
            gameEnd.SetActive(false);
            gameSettings.SetActive(false);
            gameScoreList.SetActive(false);
            //龙是否飞
            m_DragonColler.IsDragonFly(true);
            //BG和黄瓜柱子和Back的控制
            m_HGMove_1.IsMoveZhu(true);
            m_HGMove_2.IsMoveZhu(true);
            m_TestMove.IsBgMove(true);
            m_TestBackMove.IsBgMove(true);
            
            
        }
        if (gameState == GameState.END)//游戏结束
        {
            gameStart.SetActive(false);
            gaming.SetActive(false);
            gameEnd.SetActive(true);
            gameSettings.SetActive(false);
            gameScoreList.SetActive(false);
            //龙是否飞
            m_DragonColler.IsDragonFly(false);
            //BG和黄瓜柱子和Back的控制
            m_HGMove_1.IsMoveZhu(false);
            m_HGMove_2.IsMoveZhu(false);
            m_TestMove.IsBgMove(false);
            m_TestBackMove.IsBgMove(false);
           

        }
        if (gameState == GameState.SETTING)//设置界面
        {
            gameStart.SetActive(false);
            gaming.SetActive(false);
            gameEnd.SetActive(false);
            gameSettings.SetActive(true);
            gameScoreList.SetActive(false);
            //龙是否飞
            m_DragonColler.IsDragonFly(false);
            //BG和黄瓜柱子和Back的控制
            m_HGMove_1.IsMoveZhu(false);
            m_HGMove_2.IsMoveZhu(false);
            m_TestMove.IsBgMove(false);
            m_TestBackMove.IsBgMove(false);
           

        }
        if (gameState == GameState.SCORELIST)//分数排行榜
        {
            gameStart.SetActive(false);
            gaming.SetActive(false);
            gameEnd.SetActive(false);
            gameSettings.SetActive(false);
            gameScoreList.SetActive(true);
            //龙是否飞
            m_DragonColler.IsDragonFly(false);
            //BG和黄瓜柱子和Back的控制
            m_HGMove_1.IsMoveZhu(false);
            m_HGMove_2.IsMoveZhu(false);
            m_TestMove.IsBgMove(false);
            m_TestBackMove.IsBgMove(false);
            

            
        }

    }
    //开始按钮
    public void OnGameStart()
    {
        gameState = GameState.GAMING;
        //声音选项
        if (IsOnVoice)
        {
            m_Audio.Play();
        }
        else
            m_Audio.Stop();
        
    }
    //设置按钮
    public void OnSettings()
    {
        gameState = GameState.SETTING;
       
    }
    //设置结束后，完成按钮
    public void OnDone()
    {
        gameState = GameState.START;
       
    }
    //游戏结束后，在来一局按钮
    public void ResetButton()
    {

        ScoreManager.instance.SetScore(score);
        gameState = GameState.START;
        //让现实分数，和分数重置
        score = 0;

        gameScore.text = "总分数：" + score + "分";
        //让飞龙的位置也要重置
        m_FlyDragon.transform.position = new Vector3(-3f, -0.2f, -1f);
        //让柱子的位置重置
        m_HG_1.transform.localPosition = new Vector3(0.78f, -0.22f, 0f);
        m_HG_2.transform.localPosition = new Vector3(1.239f, 0f, 0f);
        
        

    }
    //点击排行榜按钮，显示
    public void ScoreListButton()
    {
        gameState = GameState.SCORELIST;
        
    }
    
     

    //游戏中，加分
    private void add_Score()
    {
        score += 1;
        gameScore.text = "总分数：" + score + "分";
    }
    //龙撞到柱子死亡
    private void Dragon_Die()
    {
        gameState = GameState.END;
        gameOver_Score.text = "总分数：" + score + "分";
        //声音选项
        m_Audio.Stop();
    }
    //开启声音
    public void OpenIsVoice()
    {
        IsOnVoice = true;
    }
    //关闭声音
    public void CloseIsVoice()
    {
        IsOnVoice = false;
    }
    //设置声音大小
    public void SetVolume(float s)
    {
        m_Audio.volume = s;
    }
}
